/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package muhnes.sizoo.dal;


import java.util.List;
import javax.annotation.PreDestroy;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import muhnes.sizoo.modelo.Especimen;
import muhnes.sizoo.HibernateUtilZoologia;
/**
 *
 * @author Ricardo Orellana
 */
public class EspecimenDaoImpl implements EspecimenDao{
    
    Session sesion;
    
    public EspecimenDaoImpl() {
        sesion = HibernateUtilZoologia.getSessionFactory().openSession();
    }
    
    @PreDestroy
    public void destroy() {
        sesion.close();
    }
    
    @Override
    public void guardar( Especimen especimen ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.save( especimen );
        transaccion.commit();
    }

    @Override
    public void guardarColeccion( List< Especimen > especimenes ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.saveOrUpdate( especimenes );
        transaccion.commit();
    }

    @Override
    public void actualizar( Especimen especimen ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.update( especimen );
        transaccion.commit();
    }

    @Override
    public void actualizarColeccion( List< Especimen > especimenes ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.update( especimenes );
        transaccion.commit();
    }

    @Override
    public void eliminar( Especimen especimen ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.delete( especimen );
        transaccion.commit();
    }

    @Override
    public void eliminarColeccion( List< Especimen > especimenes ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.delete( especimenes );
        transaccion.commit();
    }

    @Override
    public void eliminarPorId( String id ) {
        Transaction transaccion = sesion.beginTransaction();
        Especimen especimen = buscarPorId( id );
        sesion.delete( especimen );
        transaccion.commit();
    }

    @Override
    public Especimen buscarPorId( String id ) {
        Transaction transaccion = sesion.beginTransaction();
        Query consulta = sesion.createQuery("from Especimen where codigoespecimen = '" + id + "'");
        Especimen especimen = (Especimen)consulta.uniqueResult();
        transaccion.commit();
        return especimen;
    }

    @Override
    public List< Especimen > buscarTodos() {
        Transaction transaccion = sesion.beginTransaction();
        Query consulta = sesion.createQuery("from Especimen");
        List< Especimen > especimenes = consulta.list();
        transaccion.commit();
        return especimenes;
    }
    
}
